#!/bin/sh

echo "备份开始时间："
date "+%F %T"
echo

SERVER_NAME='server_name'

DB_BKPATH='/project/docker/data/mysql_main_backup'

MINIO_BKPATH='/project/docker/data/minio-main-backup'
MINIO_PATH='/project/docker/data/minio-main'

DATE=$(date +%Y%m%d)
DATETIME=$(date +%Y%m%d%H%M)
MONTH_AGO=$(date -d "-2 month" +%Y%m)
DAYY=$(date +%d)

# 数据库每日备份
echo "MySQL备份开始…………………………………………………………………………………………………………………………………………"
date "+%F %T"
echo

dbarr=(
    'smcp-core 123456 smcp-core'
    'smcp-oss 123456 smcp-oss'
    'smcp-exhibit 123456 smcp-exhibit'
    'smcp-ticket 123456 smcp-ticket'
    'smcp-sls 123456 smcp-sls'
    'smcp-cms 123456 smcp-cms'
    'smcp-external 123456 smcp-external'
    'smcp-public 123456 smcp-public'
    'fun-boot 123456 fun-boot'
)

cd ${DB_BKPATH}

mkdir ${DATETIME}

for i in "${dbarr[@]}"; do

    db=($i)

    db_user=${db[0]}
    db_pass=${db[1]}
    db_name=${db[2]}

    db_user=root
    db_pass=12345678

    echo "$db_name Start mysqldump ……"

    # 系统安装部署MySQL备份
    #/usr/bin/mysqldump --opt -u${db_user} -p${db_pass} ${DB_NAME} > ${DB_BKPATH}/${DATETIME}/${db_name}_${DATETIME}.sql

    # Docker容器部署MySQL备份
    docker exec -i mysql-main mysqldump -u${db_user} -p${db_pass} --set-gtid-purged=OFF ${db_name} | gzip >${DB_BKPATH}/${DATETIME}/${db_name}_${DATETIME}.sql.gz

    echo "$db_name Complete mysqldump"

done

echo "压缩: ${DB_BKPATH}/${DATETIME}"
tar -czvf ${DATETIME}.tar.gz ${DATETIME}

/project/docker/data/data_backup/mc cp ${DATETIME}.tar.gz minio3233/backup2022/${SERVER_NAME}/${DATE}/${DATETIME}-db-funboot-161-dag.tar.gz

rm -rf ${DB_BKPATH}/${DATETIME}

echo "删除一个月之前的备份：${DB_BKPATH}/${MONTH_AGO}*"
rm -rf ${DB_BKPATH}/${MONTH_AGO}*

echo
echo "MySQL备份完成…………………………………………………………………………………………………………………………………………"

echo
# 每月1日、10日、20日进行备份
echo "Minio备份开始…………………………………………………………………………………………………………………………………………"
date "+%F %T"
echo

if [ $DAYY -eq 1 ] || [ $DAYY -eq 10 ] || [ $DAYY -eq 20 ]; then

    cd ${MINIO_BKPATH}

    echo "压缩: ${MINIO_PATH}"
    tar -czvf ${DATETIME}.tar.gz ${MINIO_PATH}

    /project/docker/data/data_backup/mc cp ${DATETIME}.tar.gz minio3233/backup2022/${SERVER_NAME}/${DATE}/${DATETIME}-minio-161-dag.tar.gz

    rm -f ${DATETIME}.tar.gz

    echo
    echo "Minio备份完成…………………………………………………………………………………………………………………………………………"

else
    echo "今日不备份Minio"
fi

echo
# 每月1日、10日、20日进行备份
echo "Nginx备份开始…………………………………………………………………………………………………………………………………………"
date "+%F %T"
echo

if [ $DAYY -eq 1 ] || [ $DAYY -eq 10 ] || [ $DAYY -eq 20 ]; then

    cd /project/docker/data/nginx_backup

    echo "压缩: /usr/share/nginx/html"
    tar -czvf ${DATETIME}.tar.gz /usr/share/nginx/html /etc/nginx/nginx.conf

    /project/docker/data/data_backup/mc cp ${DATETIME}.tar.gz minio3233/backup2022/${SERVER_NAME}/${DATE}/${DATETIME}-nginx-161-dag.tar.gz

    rm -f ${DATETIME}.tar.gz

    echo
    echo "Nginx备份完成…………………………………………………………………………………………………………………………………………"

else
    echo "今日不备份Nginx"
fi

echo
# 每月5日修复操作系统漏洞
echo "修复操作系统漏洞开始…………………………………………………………………………………………………………………………………………"
date "+%F %T"
echo

if [ $DAYY -eq 5 ]; then

    yum upgrade -y

    echo
    echo "修复操作系统漏洞完成…………………………………………………………………………………………………………………………………………"

else
    echo "今日不修复操作系统漏洞"
fi

echo
echo "备份完成时间："
date "+%F %T"
echo
